
Partial Class EditArticle
    Inherits System.Web.UI.Page
    Dim article As New ArticleObj
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Id As Integer = 0
        If Not IsPostBack Then
            If InStr(Me.Request.RawUrl, "ArticleID") > 0 Then
                Id = CInt(Me.Request.QueryString("ArticleID"))
                article = article.GetArticleByID(Id)
                txtTitle.Text = article.Title
                teArticleBody.Value = article.Text
                txtCreation.Text = article.CreationDate
                txtPost.Text = article.PostDate
                txtExpire.Text = article.ExpirationDate
                chkApprove.Checked = article.Approved
                ddlCategory.SelectedValue = article.CategoryID
                txtUser.Text = article.UserID
                lblID.Text = article.ArticleID.ToString
            Else
                txtCreation.Text = Date.Now
                txtUser.Text = HttpContext.Current.Profile.UserName
                chkApprove.Checked = True
                lblID.Text = 0
            End If
        End If

        If HttpContext.Current.Profile.UserName = "admin" Then
            chkApprove.Enabled = True
        Else
            chkApprove.Enabled = False
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not lblID.Text = "0" Then
            article.ArticleID = CInt(lblID.Text)
            PopulateObject()
            article.UpdateArticle(article)
        Else
            PopulateObject()
            lblID.Text = article.GetNextID.ToString
            article.CreateArticle(article)

        End If
    End Sub

    Protected Sub PopulateObject()
        article.Approved = chkApprove.Checked
        article.CategoryID = ddlCategory.SelectedValue
        article.CreationDate = txtCreation.Text
        article.ExpirationDate = txtExpire.Text
        article.PostDate = txtPost.Text
        article.Text = teArticleBody.Value
        article.Title = txtTitle.Text
        article.UserID = txtUser.Text
    End Sub
End Class
